package com.syp.jrx.batch.writer;

import com.syp.jrx.model.Statistics;
import org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider;
import org.springframework.batch.item.database.JdbcBatchItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Service;

import javax.sql.DataSource;

@Service
@Configuration
public class WriterService {
    @Autowired
    private DataSource dataSource;

    @Bean
    public  JdbcBatchItemWriter<Statistics> writerDatabase() {
        JdbcBatchItemWriter<Statistics> writer = new JdbcBatchItemWriter<>();
        //设置数据源
        writer.setDataSource( dataSource );
        //设置SQL
        writer.setSql("INSERT INTO day_statistics(s_index,cust_id,update_time,trans_date,surname,tran_max_amt," +
                "pay_amt,tran_cnt,pay_cnt,tran_amt) VALUES (:s_index,:cust_id,:update_time,:trans_date,:surname," +
                ":tran_max_amt,:pay_amt,:tran_cnt,:pay_cnt,:tran_amt)");
        //设置参数
        writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<Statistics>());
        return writer;
    }
}
